﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.IO;

namespace PagedDataGridControls
{
    /// <summary>
    /// Interaction logic for DetailView.xaml
    /// </summary>
    public partial class DetailView : UserControl
    {

        #region Detail view properties
        /// <summary>
        /// Header width
        /// </summary>
        public int DetailHeaderWidth
        {
            get { return (int)GetValue(DetailHeaderWidthProperty); }
            set { SetValue(DetailHeaderWidthProperty, value); }
        }
        public static readonly DependencyProperty DetailHeaderWidthProperty =
            DependencyProperty.Register("DetailHeaderWidth", typeof(int), typeof(PagedDataGrid),
            new UIPropertyMetadata(100));

        /// <summary>
        /// Min Content width
        /// </summary>
        public int MinDetailContentWidth
        {
            get { return (int)GetValue(MinDetailContentWidthProperty); }
            set { SetValue(MinDetailContentWidthProperty, value); }
        }
        public static readonly DependencyProperty MinDetailContentWidthProperty =
            DependencyProperty.Register("MinDetailContentWidth", typeof(int), typeof(PagedDataGrid),
            new UIPropertyMetadata(300));

        /// <summary>
        /// Max Content width
        /// </summary>
        public int MaxDetailContentWidth
        {
            get { return (int)GetValue(MaxDetailContentWidthProperty); }
            set { SetValue(MaxDetailContentWidthProperty, value); }
        }
        public static readonly DependencyProperty MaxDetailContentWidthProperty =
            DependencyProperty.Register("MaxDetailContentWidth", typeof(int), typeof(PagedDataGrid),
            new UIPropertyMetadata(500));

        /// <summary>
        /// Content height
        /// </summary>
        public int DetailContentHeight
        {
            get { return (int)GetValue(DetailContentHeightProperty); }
            set { SetValue(DetailContentHeightProperty, value); }
        }
        public static readonly DependencyProperty DetailContentHeightProperty =
            DependencyProperty.Register("DetailContentHeight", typeof(int), typeof(PagedDataGrid),
            new UIPropertyMetadata(150));

        /// <summary>
        /// Image width
        /// </summary>
        public int DetailImageWidth
        {
            get { return (int)GetValue(DetailImageWidthProperty); }
            set { SetValue(DetailImageWidthProperty, value); }
        }
        public static readonly DependencyProperty DetailImageWidthProperty =
            DependencyProperty.Register("DetailImageWidth", typeof(int), typeof(PagedDataGrid),
            new UIPropertyMetadata(480));

        /// <summary>
        /// Image width
        /// </summary>
        public int DetailImageHeight
        {
            get { return (int)GetValue(DetailImageHeightProperty); }
            set { SetValue(DetailImageHeightProperty, value); }
        }
        public static readonly DependencyProperty DetailImageHeightProperty =
            DependencyProperty.Register("DetailImageHeight", typeof(int), typeof(PagedDataGrid),
            new UIPropertyMetadata(360));
        #endregion

        public DetailView()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(DetailView_Loaded);
        }

        void DetailView_Loaded(object sender, RoutedEventArgs e)
        {
           
        }

        /// <summary>
        /// Show DataRow in detail List
        /// </summary>
        /// <param name="headerList"></param>
        /// <param name="view"></param>
        public void SetMyData(List<string> headerList, DataRowView view)
        {
            if (detailPanel.Children.Count > 0)
            {
                detailPanel.Children.RemoveRange(0, detailPanel.Children.Count);
            }


            for (int i = 0; i < view.Row.ItemArray.Count(); i++)
            {
                StackPanel detailViewRootContainerPanel = new StackPanel();
                detailViewRootContainerPanel.Margin = new Thickness(2, 2, 0, 0);
                detailViewRootContainerPanel.Orientation = Orientation.Horizontal;
                TextBlock block = new TextBlock();
                block.Text = headerList[i];
                block.Width = this.DetailHeaderWidth;
                detailViewRootContainerPanel.Children.Add(block);

                //Here if the data is image then parse the byte[] ,otherwise process it as text
                if (view.Row.ItemArray[i].GetType() == typeof(byte[]))
                {
                    byte[] imageData = view.Row.ItemArray[i] as byte[];
                    if (null != imageData)
                    {
                        MemoryStream byteStream = new MemoryStream(imageData);
                        byteStream.Seek(0, SeekOrigin.Begin);
                        //BitmapDecoder decoder = new PngBitmapDecoder(byteStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);

                        BitmapImage image = new BitmapImage();

                        image.BeginInit();
                        image.StreamSource = byteStream;
                        image.EndInit();
                        Image imageControl = new Image();

                        imageControl.Width = this.ActualWidth-120;
                        imageControl.Height = DetailImageHeight;
                        imageControl.Stretch = Stretch.Fill;
                        imageControl.Source = image;
                        detailViewRootContainerPanel.Children.Add(imageControl);
                    }
                }
                else
                {
                    TextBox box = new TextBox();
                    box.MinWidth = MinDetailContentWidth;
                    box.MaxWidth = MaxDetailContentWidth;
                    box.Text = view.Row.ItemArray[i].ToString();
                    box.TextWrapping = TextWrapping.Wrap;
                    box.IsReadOnly = true;
                    detailViewRootContainerPanel.Children.Add(box);
                }

                DockPanel.SetDock(detailViewRootContainerPanel, Dock.Top);
                detailPanel.Children.Add(detailViewRootContainerPanel);
            }
        }
    }
}
